package io.xiyou.user.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import io.xiyou.user.api.SentinelService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 账户接口
 *
 * @author 西柚
 * @date 2021-10-11 16:57:39
 */
@Validated
@RestController
@RequestMapping("/sentinel")
public class SentinelController {

    @Reference
    SentinelService sentinelService;//proxy$0

    @GetMapping("/say")
    @SentinelResource(value = "/sentinel/say", blockHandler = "flowException")
    public String sayHello() {
        return sentinelService.sayHello("test");
    }

    @GetMapping("/say2")
    @SentinelResource(value = "/sentinel/say2", blockHandler = "degradeException")
    public String sayHello2() {
        return sentinelService.sayHello("test2");
    }

    // 自定义异常必须与原方法返回值及参数完全一致
    public String flowException(BlockException e) {
        return "限流了！";
    }

    // 自定义异常必须与原方法返回值及参数完全一致
    public String degradeException(BlockException e) {
        return "降级了！";
    }

}
